package main;



public class MathFunction 
{
	public String name = "";
	public String exp = "";
	public String var = "";
	
	public MathFunction(String exp, String name, String var)
	{
		this.name = name;
		this.exp = exp;
		this.var = var;
	}
	
	public double[] getRootsBisect(MathFunction f, double a, double b, int kmax, double tol)
	{
		int k = 1;
		double x[] = new double[kmax+1];
		
		while(k <= kmax)
		{
			x[k] = (a+b) / 2;
			
			if(f.getValueAt(x[k]) == 0 || Math.abs(b-a)/2 < tol)
				break;
			
			if(f.getValueAt(a) * f.getValueAt(x[k]) < 0)
				b = x[k];
			else
				a = x[k];
			
			k++;
		}
		x[0] = k;
		return x;
	}
	
	public double getValueAt(Double val)
	{		
		String varFunc = exp.replaceAll("(?<![a-zA-Z])" + this.var + "(?![a-zA-Z])", "(" + Double.toString(val) + ")");
		String result = MathBasicArithmetic.eval(varFunc);
		return Double.parseDouble(result);
	}
	
}
